<template>
  <el-card>
    <el-button
      type="primary"
      :icon="Plus"
      @click="showSaveAttr"
      :disabled="!CategoryStore.category3Id"
      >添加属性</el-button
    >
    <el-table border :data="attrList">
      <el-table-column label="序号" type="index" align="center" width="60" />
      <el-table-column label="属性名称" prop="attrName" width="100" />
      <el-table-column label="属性值列表">
        <template v-slot="{ row }">
          <el-tag
            type="success"
            class="attr-tag"
            v-for="attrValue in row.attrValueList"
            :key="attrValue.id"
            >{{ attrValue.valueName }}</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template v-slot="{ row }">
          <el-tooltip content="修改属性" placement="bottom" effect="light">
            <el-button
              type="primary"
              :icon="Edit"
              @click="showUpdateAttr(row)"
            ></el-button>
          </el-tooltip>
          <el-tooltip content="删除属性" placement="bottom" effect="light">
            <div class="attr-btn-wrap">
              <el-popconfirm
                @confirm="removeAttr(row.id)"
                :title="`你确定删除${row.attrName}吗`"
              >
                <template #reference>
                  <el-button type="danger" :icon="Delete"></el-button>
                </template>
              </el-popconfirm>
            </div>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "AttrList",
};
</script>

<script lang="ts" setup>
import { ref, watch } from "vue";
import { Plus, Edit, Delete } from "@element-plus/icons-vue";
import { getAttrListApi } from "@/api/product/attrList";
import { useCategoryStore } from "@/stores/category";
import type { AttrList, SaveAttrItem } from "@/api/product/model/attrListModel";
import { deleteAttrApi } from "@/api/product/attrList";
import { ElMessage } from "element-plus";

const emit = defineEmits(["setIsShowAttrList", "setAttr"]);
const CategoryStore = useCategoryStore();
const attrList = ref<AttrList>([]);
//添加显示
const showSaveAttr = () => {
  emit("setAttr", {
    attrName: "",
    attrValueList: [],
  });
  emit("setIsShowAttrList", false);
};
//更新显示
const showUpdateAttr = (row: SaveAttrItem) => {
  emit("setAttr", row);
  emit("setIsShowAttrList", false);
};
const getAttrList = async () => {
  const { category3Id, category2Id, category1Id } = CategoryStore;
  attrList.value = await getAttrListApi({
    category1Id: category1Id as number,
    category2Id: category2Id as number,
    category3Id: category3Id as number,
  });
};
watch(
  () => CategoryStore.category3Id,
  () => {
    const { category3Id, category2Id, category1Id } = CategoryStore;
    if (!category3Id) {
      attrList.value = [];
      return;
    }
    getAttrList();
  },
  { immediate: true } //返回AttrList组件实现数据请求}
);
const removeAttr = async (id: number) => {
  await deleteAttrApi(id);
  ElMessage.success("删除成功");
  getAttrList();
};
</script>

<style scoped>
.attr-tag {
  margin-right: 10px;
}
.attr-btn-wrap{
  display: inline-block;
  margin-left: 10px;
}
</style>
